﻿using Bot_Schedule.Lib;
using Codeplex.Data;
using DevExpress.XtraEditors;
using DevExpress.XtraTreeList;
using DevExpress.XtraTreeList.Nodes;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Net;
using System.Text;
using System.Windows.Forms;

namespace Bot_Schedule
{
    public partial class frm_danhsachpage : XtraForm
    {
        private DataTable dtWebsite;
        private DataTable dtpageUrl;
        private ListView.SelectedListViewItemCollection seleteditem;
        public frm_danhsachpage(DataTable dtWebsite, DataTable dtpageUrl, ListView.SelectedListViewItemCollection selectedListViewItemCollection)
        {
            this.dtWebsite = dtWebsite;
            this.dtpageUrl = dtpageUrl;
            this.seleteditem = selectedListViewItemCollection;
            InitializeComponent();
            loadtreelist();
        }
        void loadtreelist()
        {
            treeList1.Nodes.Clear();
            foreach (DataRow row in dtWebsite.Rows)
            {
                TreeListNode par = treeList1.Nodes.Add(new object[] { row["wsURL"].ToString() });
                par.Tag = row["Stt"].ToString();
                DataRow[] rows = dtpageUrl.Select("webID = '" + row["Stt"].ToString() + "'");
                foreach (DataRow subrow in rows)
                {
                    TreeListNode node = par.Nodes.Add(new object[] { subrow["PageUrl"].ToString() });
                    node.Tag = subrow["Stt"].ToString();
                    if (compare(subrow["Stt"].ToString())) node.Checked = true;
                }
            }
            treeList1.ExpandAll();
        }
        bool compare(string input)
        {
            bool result = false;
            if (seleteditem.Count == 1)
            {
                string[] data = seleteditem[0].SubItems[2].Text.Split(',');
                if (data.Length > 0)
                {
                    foreach (string item in data)
                    {
                        if (input == item)
                        {
                            result = true;
                            break;
                        }
                    }
                }
                else
                {
                    if (input == seleteditem[0].SubItems[2].Text) result = true;
                }
            }
            return result;
        }
        string ids()
        {
            string res = "'',";
            foreach (ListViewItem item in seleteditem)
            {
                res = res + "'" + item.Tag.ToString() + "',";
            }
            res = res.Substring(0, res.Length - 1);
            return res;
        }
        string dsWebs()
        {
            string res = "";
            foreach (TreeListNode node in treeList1.Nodes)
            {
                if (node.HasChildren)
                {
                    foreach (TreeListNode snode in node.Nodes)
                    {
                        if (snode.Checked)
                            res = res + snode.Tag.ToString() + ",";
                    }
                }
            }
            if (res.Length > 0) res = res.Substring(0, res.Length - 1);
            return res;
        }
        void action()
        {
            var obj = new
                {
                    ids = ids(),
                    dsWebs = dsWebs()
                };
            var json = DynamicJson.Serialize(obj);
            string resu = Functions.getdataReq(new WebClient(), "updatebotsite", json);
            if (resu == "1")
            {
                this.DialogResult = DialogResult.OK;
            }
            else MessageBox.Show("Sự cố khi thao tác với CSDL");
        }
        private void btChapnhan_Click(object sender, EventArgs e)
        {
            action();
            this.Close();
        }

        private void btXoaAll_Click(object sender, EventArgs e)
        {
            treeList1.UncheckAll();
        }

        private void btChonAll_Click(object sender, EventArgs e)
        {
            treeList1.CheckAll();
        }

        private void btHuy_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
